﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Portal.Presentation.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Portal.Logic.Services;
using FluentAssertions;
using System.Web.Mvc;
using Portal.Presentation.Models;
using Rhino.Mocks;
using Portal.Model;
using Portal.Logic.Services.SkillsCatalog;
using Portal.Models.SkillsCatalog;

namespace Portal.Presentation.Controllers.Tests.Units
{
	[TestClass()]
	public class DashboardControllerTests : BaseControllerTests<DashboardController>
	{
		private IUserInfoService _userInfoService;
		private ICurriculumVitaeService _curriculumVitaeService;

		protected override void InitializeTest()
		{
			base.InitializeTest();
			_userInfoService = Mocks.DynamicMock<IUserInfoService>();
			_curriculumVitaeService = Mocks.DynamicMock<ICurriculumVitaeService>();

		}

		#region Index
		[TestMethod()]
		public void Index_UserInfoIsNotValid_ResultHasWarning()
		{
			var info = Mocks.DynamicMock<IUserInfo>();
			Expect.Call(_userInfoService.GetBy(null)).IgnoreArguments().Return(info);
			Expect.Call(_userInfoService.IsValid(info)).Return(false);

			MocksValidCurriculumVitae();
			Mocks.ReplayAll();

			var controller = GetController();
			var actionResult = controller.Index();
			var result = actionResult as ViewResult;
			var resutlModel = result.Model as DashboardViewModel;

			resutlModel.Warnings.Count().Should().Be(1);
		}

		[TestMethod()]
		public void Index_UserInfoIsValid_ResultWithoutWarning()
		{
			MocksValidUserInfo();
			MocksValidCurriculumVitae();
			Mocks.ReplayAll();

			var controller = GetController();
			var actionResult = controller.Index();
			var result = actionResult as ViewResult;
			var resutlModel = result.Model as DashboardViewModel;

			resutlModel.Warnings.Count().Should().Be(0);
		}

		[TestMethod()]
		public void Index_UserInfoIsNull_ResultWithoutWarning()
		{
			Expect.Call(_userInfoService.GetBy(null)).IgnoreArguments().Return(null);
			MocksValidCurriculumVitae();
			Mocks.ReplayAll();

			var controller = GetController();
			var actionResult = controller.Index();
			var result = actionResult as ViewResult;
			var resutlModel = result.Model as DashboardViewModel;

			resutlModel.Warnings.Count().Should().Be(0);

		}

		[TestMethod()]
		public void Index_CVIsNull_ResultHasWarning()
		{
			Expect.Call(_curriculumVitaeService.GetBy(null)).IgnoreArguments().Return(null);

			MocksValidUserInfo();
			Mocks.ReplayAll();

			var controller = GetController();
			var actionResult = controller.Index();
			var result = actionResult as ViewResult;
			var resutlModel = result.Model as DashboardViewModel;

			resutlModel.Warnings.Count().Should().Be(1);
		}

		[TestMethod()]
		public void Index_CVIsUpdated_ResultWithoutWarning()
		{
			var cv = Mocks.DynamicMock<ICurriculumVitae>();
			Expect.Call(_curriculumVitaeService.IsUpToDate(cv, DateTime.Now)).IgnoreArguments().Return(true);
			Expect.Call(_curriculumVitaeService.GetBy(null)).IgnoreArguments().Return(cv);

			MocksValidUserInfo();
			Mocks.ReplayAll();

			var controller = GetController();
			var actionResult = controller.Index();
			var result = actionResult as ViewResult;
			var resutlModel = result.Model as DashboardViewModel;

			resutlModel.Warnings.Count().Should().Be(0);
		}

		[TestMethod()]
		public void Index_CVIsOutOfDate_ResultHasWarning()
		{
			var cv = Mocks.DynamicMock<ICurriculumVitae>();
			Expect.Call(_curriculumVitaeService.IsUpToDate(cv, DateTime.Now)).IgnoreArguments().Return(false);
			Expect.Call(_curriculumVitaeService.GetBy(null)).IgnoreArguments().Return(cv);

			MocksValidUserInfo();
			Mocks.ReplayAll();

			var controller = GetController();
			var actionResult = controller.Index();
			var result = actionResult as ViewResult;
			var resutlModel = result.Model as DashboardViewModel;

			resutlModel.Warnings.Count().Should().Be(1);

		}

		private void MocksValidCurriculumVitae()
		{
			var cv = Mocks.DynamicMock<ICurriculumVitae>();
			Expect.Call(_curriculumVitaeService.GetBy(null)).IgnoreArguments().Return(cv);
			Expect.Call(_curriculumVitaeService.IsUpToDate(cv, DateTime.Now)).IgnoreArguments().Return(true);
		}

		private void MocksValidUserInfo()
		{
			var info = Mocks.DynamicMock<IUserInfo>();
			Expect.Call(_userInfoService.GetBy(null)).IgnoreArguments().Return(info);
			Expect.Call(_userInfoService.IsValid(info)).Return(true);
		}
		#endregion

		protected override DashboardController GetController()
		{
			return new DashboardController(SecurityProvider, SessionService, _userInfoService, _curriculumVitaeService);

		}
	}
}
